use crate::tables::*;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.create_table(Registry::table()).await?;
        manager.create_table(Position::table()).await?;
        manager.create_table(Department::table()).await?;
        manager.create_table(Personnel::table()).await?;
        manager.create_table(Resignation::table()).await?;

        manager.create_foreign_key(Department::with_parent()).await?;
        manager.create_foreign_key(Position::under_department()).await?;
        manager.create_foreign_key(Personnel::in_department()).await?;
        manager.create_foreign_key(Personnel::at_position()).await?;
        manager.create_foreign_key(Personnel::has_resignation()).await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.drop_table(Registry::drop()).await?;
        manager.drop_table(Position::drop()).await?;
        manager.drop_table(Department::drop()).await?;
        manager.drop_table(Personnel::drop()).await?;
        manager.drop_table(Resignation::drop()).await?;

        Ok(())
    }
}
